package com.htht.smartschool.amq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息发送器
 * @author wangy
 *
 */
public class MessageSender {

	/**会话**/
	private Session m_session;
	/**连接工厂**/
	private ConnectionFactory m_ConnectionFactory;
	/**连接**/
	private Connection m_Connection;
	/**生产者**/
	private MessageProducer m_Producer;
	/**服务地址**/
	private String m_Url;
	/**消息目的地**/
	private String m_Target;
	/**是否持久**/
	private boolean m_isPersist;
	/**消息发送类型(0:队列,1:广播)**/
	private int m_senderType;
	/**发送消息者的ID**/
	private String m_clientID;
	/**是否已经启动**/
	private boolean m_isStart;
	
	
	
	public static void main(String[] args) throws Exception {
		String url = "tcp://172.16.0.109:61616";
		String target = "testMQ";
		boolean isPersist = true;
		int senderType = 0;
		String clientID ="";
		
		String textString = "王远发送消息！！";
		
		MessageSender msgS = new MessageSender(url, target, isPersist, senderType, clientID);
		
		msgS.start();
		msgS.sendText(textString);
		msgS.stop();
		
	}
	
	/**
	 * 发送器有参构造函数
	 * @param url ActiveMQ服务地址
	 * @param target 消息目的地
	 * @param isPersist  是否持久化
	 * @param senderType  消息发送方式(0:队列,点对点 1:广播)
	 * @param clientID 客户端ID,
	 */
	public MessageSender(String url,String target,boolean isPersist,int senderType,String clientID){
		m_Url = url;
		m_Target = target;
		m_isPersist = isPersist;
		m_senderType = senderType;
		m_clientID = clientID;
		m_isStart = false;
		
		m_ConnectionFactory = null;
		m_Connection = null;
		m_Producer = null;
		m_session = null;
	}
	/**
	 * 启动消息发送器
	 */
	public void start(){
		try {
			if(m_isStart)
				throw new Exception("已经启动");
			if(m_Url==null||m_Url.isEmpty()){
				throw new Exception("消息服务器地址为空！");
			}
			if(m_Target==null||m_Target.isEmpty()){
				throw new Exception("消息目的地为空");
			}
			//创建链接工厂
			m_ConnectionFactory = new ActiveMQConnectionFactory(m_Url);
			//创建连接
			m_Connection = m_ConnectionFactory.createConnection();
			//创建会话
			m_session = m_Connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
			 //根据发送器类型创建消息生产者    0为队列,1为广播
			switch (m_senderType){
				case 0:
					Destination destinationQ = m_session.createQueue(m_Target);
					m_Producer = m_session.createProducer(destinationQ);
					if(m_isPersist){
						m_Producer.setDeliveryMode(DeliveryMode.PERSISTENT);//需要持久化
					}else {
						m_Producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//不持久化
					}
					break;
				case 1:	
					Destination destinationT = m_session.createTopic(m_Target);
					m_Producer = m_session.createProducer(destinationT);
					if(m_isPersist){
						m_Producer.setDeliveryMode(DeliveryMode.PERSISTENT);//需要持久化
					}else {
						m_Producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//不持久化
					}
					break;
					
				default:
					throw new Exception("未知道消息发送器的类型");
			}
			m_isStart =true;
			System.out.println("消息发送器实例化完毕,等待消息发送操作!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送消息
	 * @param content  消息内容
	 * @param consumerId
	 */
	public void sendText(String content){
		try {
			if (!m_isStart || m_Producer == null)
			        throw new Exception("消息发送器未启动！");
			if (content.isEmpty())
                throw new Exception("需要发送的消息为空！");
			TextMessage message = m_session.createTextMessage();
			message.setText(content);
			if(!m_clientID.isEmpty()){
				message.setJMSType(m_clientID);//此属性可用来决定谁来接收消息
			}
			m_Producer.send(message);
			m_session.commit();
//			String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
//			System.out.println("消息成功提交,服务地址为:"+m_Url+";队列名为:"+m_Target+"系统时间:"+date);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 停止消息服务
	 * @throws JMSException
	 */
	public void stop() throws JMSException{
		if (!m_isStart)
            return;
        if (m_Producer != null)
            m_Producer = null;
        if (m_session != null)
        {
            m_session.close();
            m_session = null;
        }
        if (m_Connection != null)
        {
            m_Connection.close();
            m_Connection = null;
        }
        if (m_ConnectionFactory != null)
            m_ConnectionFactory = null;
        
        m_isStart = false;
	}
	public String getM_Url() {
		return m_Url;
	}
	public void setM_Url(String mUrl) {
		m_Url = mUrl;
	}
	public String getM_Target() {
		return m_Target;
	}
	public void setM_Target(String mTarget) {
		m_Target = mTarget;
	}
	public boolean isM_isPersist() {
		return m_isPersist;
	}
	public void setM_isPersist(boolean mIsPersist) {
		m_isPersist = mIsPersist;
	}
	public int getM_senderType() {
		return m_senderType;
	}
	public void setM_senderType(int mSenderType) {
		m_senderType = mSenderType;
	}

}
